const glob = require('glob');
const projectName = process.argv[3];
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);

function getPages() {
  let entries = {};
  if (IS_PROD) {
    if (!projectName) {
      console.error('请正确输入要打包的模块名');
      process.exit();
    }
    console.log(`正在打包模块：${projectName}`);
    entries[projectName] = {
      // page的入口
      entry: `src/pages/${projectName}/main.js`,
      // 模板来源
      template: 'public/index.html',
      // 在 dist/index.html 的输出
      filename: projectName + '.html',
      title: projectName,
      chunks: ['chunk-vendors', 'chunk-common', projectName],
    };
  } else {
    const pages = glob.sync('./src/pages/*/main.js');
    if (projectName) {
      if (!pages.includes(`./src/pages/${projectName}/main.js`)) {
        console.error('请输入正确的模块名');
        process.exit();
      }
      entries[projectName] = {
        entry: `src/pages/${projectName}/main.js`,
        filename: 'index.html',
      };
    } else {
      for (let i in pages) {
        let filepath = pages[i];
        let fileList = filepath.split('/');
        let fileName = fileList[fileList.length - 2];
        entries[fileName] = {
          entry: `src/pages/${fileName}/main.js`,

          // 模板来源
          template: `public/index.html`,
          // 在 dist/index.html 的输出
          filename: `${fileName}.html`,
          // 提取出来的通用 chunk 和 vendor chunk。
          chunks: ['chunk-vendors', 'chunk-common', fileName],
        };
      }
    }
  }
  return entries;
}


module.exports.getPages = getPages;
module.exports.moduleName = projectName;
